#ifndef STOPWATCH_HPP
#define STOPWATCH_HPP

#include "error.hpp"
#include <ctime>

class stopwatch_c
{
        enum
        {
                DESIGNED_CLOCKS_IN_SECOND       = 1000,
                CLOCKS_IN_SECOND                = CLOCKS_PER_SEC,
        };

        clock_t         clocks_started;
        clock_t         clocks_stopped;
        bool            is_stopped;

public:
        stopwatch_c ( void ) :
                clocks_started ( 0 ),
                clocks_stopped ( 0 ),
                is_stopped     ( true )
        {
                throw_if ( DESIGNED_CLOCKS_IN_SECOND != CLOCKS_IN_SECOND, ERROR_INVALID_CLOCK_RESOLUTION );
        }
        
        void reset ( void )
        {
                clocks_started  = 0;
                clocks_stopped  = 0;
                is_stopped      = true;
        }

        void start ( void )
        {
                clocks_stopped = clocks_started = clock ();
                is_stopped = false;
        }

        void stop ( void )
        {
                clocks_stopped = clock ();
                is_stopped = true;
        }

        long get_ms ( void ) const
        {
                if ( is_stopped )
                {
                        return clocks_stopped - clocks_started;
                }
                else
                {
                        return clock () - clocks_started;
                }
        }
};

#endif
